<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1> 面试题</h1>
    <p>
        <!-- https://cloud.tencent.com/developer/article/1768943 -->
        红灯3秒亮一次，绿灯1秒亮一次，黄灯2秒亮一次；如何使用Promise让三个灯不断交替重复亮灯？
    </p>
</body>

<script>
function red(){
    console.log('red');
}
function green(){
    console.log('green');
}
function yellow(){
    console.log('yellow');
}

let myTask = (timer, cb) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      cb();
      resolve();
    }, timer);
  });
};

// function test(){
//     myTask(3000, red)
//     .then( function(){
//         return myTask(1000, green)
//     }).then( function(){
//         return myTask(2000, yellow)
//     }).then(function(){
//         test();  //循环
//     })
// }

async function test(){
   await myTask(3000, red);
   await myTask(1000, green);
   await myTask(2000, yellow);
   test();
}
test();

</script>
</html>